package com.stylefeng.guns.rest.modular.wz.api.back;

import com.stylefeng.guns.rest.common.base.controller.ApiBaseController;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.modular.auth.util.JwtTokenUtil;
import com.stylefeng.guns.rest.modular.wz.cache.CacheService;
import com.stylefeng.guns.rest.modular.wz.controller.TextScanService;
import com.stylefeng.guns.rest.modular.wz.service.impl.WzAdvServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 新闻模块
 */
@RestController
@RequestMapping("/wz/api/v1/news/")
public class WzNewsApi extends ApiBaseController {
    @Autowired
    private WzNewsMapper newsMapper;
    @Autowired
    private WzUserAdvCollectionMapper userAdvCollectionMapper;
    @Autowired
    private WzAdvCommentMapper advCommentMapper;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private WzUserMapper userMapper;
    @Autowired
    private PfReportMapper reportMapper;
    @Autowired
    private WzAdvServiceImpl advService;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private TextScanService textScanService;
    @Autowired
    private WzNewsUserReadMapper newsUserReadMapper;

    /**
     * 新闻列表
     */
//    @PostMapping("list")
//    public Res list(@RequestBody ParamMap paramMap){
//        int pageNum = paramMap.getIntMisValid("pageNum");
//        int pageSize = paramMap.getIntMisValid("pageSize");
//        String search = paramMap.getStrValid("search");
//        EntityWrapper<WzNews> entityWrapper = new EntityWrapper<>();
//        if(!StringUtils.isBlankStr(search)){
//            entityWrapper.like("title",search);
//        }
//        String sql = "avatar_url As avatarUrl,nick_name As nickName,user_code As userCode,is_enlarge as isEnlarge,adv_id As advId,title,cover_url As coverUrl,has_detail As hasDetail,questionnaire_id As questionnaireId,subtype,collection_number As collectionNumber,like_number As likeNumber,create_time As releaseTime";
//        entityWrapper.setSqlSelect(sql).in("status",new Integer[]{EnWzAdv.STATUS_RELEASE_no.getIndex(),EnWzAdv.STATUS_RELEASE_yes.getIndex()}).orderBy("release_time",false);
//        List<Map<String,Object>>  news = newsMapper.selectMapsPage(new Page<>(pageNum,pageSize),entityWrapper);
//        return Res.success(paramMap.data(news));
//    }

    /**
     * 新闻详情
     */
//    @PostMapping("detail")
//    public Res detail(@RequestBody ParamMap paramMap){
//        String advId = paramMap.getStrMisValid("advId");
//        String userCode = paramMap.getStrValid("userCode");
//        String sql = "share_link As shareLink,create_time As releaseTime,avatar_url As avatarUrl,nick_name As nickName,user_code As userCode,adv_id As advId,title As title,cover_url As coverUrl,adv_detail As advDetail,subtype As subtype,collection_number As collectionNumber,like_number As likeNumber";
//        List<Map<String,Object>>  news = newsMapper.selectMaps(new EntityWrapper<WzNews>().setSqlSelect(sql).eq("adv_id",advId));
//        if(news.size() <= 0){
//            return Res.fail(BizExceptionEnum.NEWS_SHIXIAO);
//        }
//        if(StringUtils.isBlankStr(userCode)){
//            news.get(0).put("hasCollection",0);
//            news.get(0).put("hasLike",0);
//        }else{
//            int count = userAdvCollectionMapper.selectCount(new EntityWrapper<WzUserAdvCollection>().eq("user_code",userCode).eq("adv_id",advId).eq("type",EnWzUser.USER_ADV_Collection.getIndex()));
//            if(count > 0){
//                news.get(0).put("hasCollection",1);
//            }else{
//                news.get(0).put("hasCollection",0);
//            }
//            int counts = userAdvCollectionMapper.selectCount(new EntityWrapper<WzUserAdvCollection>().eq("user_code",userCode).eq("adv_id",advId).eq("type",EnWzUser.USER_ADV_Like.getIndex()));
//            if(counts > 0){
//                news.get(0).put("hasLike",1);
//            }else{
//                news.get(0).put("hasLike",0);
//            }
//        }
//        int advCommentNumber = advCommentMapper.selectCount(new EntityWrapper<WzAdvComment>().eq("adv_id",advId));
//        news.get(0).put("advCommentNumber",advCommentNumber);
//        //新闻点击量统计
//        WzNews news1 = newsMapper.selectById(Long.parseLong(advId));
//        news1.setReadNumber(news1.getReadNumber() + 1);//阅读量
//        if(StringUtils.isNotBlank(userCode)){
//            int c = newsUserReadMapper.selectCount(new EntityWrapper<WzNewsUserRead>().eq("user_code",userCode).eq("adv_id",advId));
//            if(c <= 0){
//                WzNewsUserRead newsUserRead = new WzNewsUserRead();
//                newsUserRead.setCreateTime(new Date());
//                newsUserRead.setAdvId(advId);
//                newsUserRead.setUserCode(userCode);
//                newsUserReadMapper.insert(newsUserRead);
//                news1.setClicksNumber(news1.getClicksNumber() + 1);
//            }
//        }
//        newsMapper.updateById(news1);
//        return Res.success(news.get(0));
//    }

    /**
     * 新闻评论列表
     */
//    @PostMapping("commentList")
//    public Res newsCommentList(@RequestBody ParamMap paramMap){
//        int pageNum = paramMap.getIntMisValid("pageNum");
//        int pageSize = paramMap.getIntMisValid("pageSize");
//        String advId = paramMap.getStrMisValid("advId");
//        String advCommentsSql = "adv_comment_id As advCommentId,user_code As userCode,content,nick_name As nickName,avatar_url As avatarUrl,create_time As createTime";
//        List<Map<String,Object>>  advComments = advCommentMapper.selectMapsPage(new Page<>(pageNum,pageSize),new EntityWrapper<WzAdvComment>().setSqlSelect(advCommentsSql).eq("adv_id",advId).orderBy("create_time",false));
//        return Res.success(paramMap.data(advComments));
//    }

    /**
     * 增加新闻评论
     */
//    @PostMapping("commentAdd")
//    public Res commentAdd(@RequestBody ParamMap paramMap) throws Exception{
//        String advId = paramMap.getStrMisValid("advId");
//        String content = paramMap.getStrMisValid("content");
//        String replyUserCode = paramMap.getStrValid("replyUserCode");
////        boolean r = textScanService.textScan(content);
////        if(!r){
////            return Res.fail("请文明评论!");
////        }
//        Long userCode = jwtTokenUtil.getUserCodeFromToken();
//        WzUser myUser = cacheService.user(userCode);
//        if(myUser.getStatus() == 0){
//            return Res.fail("账户冻结中!");
//        }
//        //新建评论
//        WzAdvComment advComment = new WzAdvComment();
//        advComment.setAdvId(Long.parseLong(advId));
//        advComment.setAvatarUrl(myUser.getAvatarUrl());
//        advComment.setContent(content);
//        advComment.setCreateTime(new Date());
//        advComment.setUserCode(userCode);
//        if(StringUtils.isBlankStr(replyUserCode)){//评论
//            advComment.setNickName(myUser.getNickName());
//        }else{//回复
//            WzUser replyUser = cacheService.user(Long.parseLong(replyUserCode));
//            advComment.setNickName(myUser.getNickName() +" 回复 "+replyUser.getNickName());
//            advComment.setReplyUserCode(Long.parseLong(replyUserCode));
//            advComment.setMainUserCode(Long.parseLong(replyUserCode));
//        }
//        int rr = advCommentMapper.insert(advComment);
//        if(rr > 0){
//            WzNews news = newsMapper.selectById(Long.parseLong(advId));
//            news.setReleaseTime(new Date());
//            rr = newsMapper.updateById(news);
//            if(rr > 0){
//                return Res.success();
//            }
//        }
//        return Res.fail();
//    }

    /**
     * 增加新闻评论
     */
//    @PostMapping("commentAdd")
//    public Res commentAdd(@RequestBody ParamMap paramMap) throws Exception{
//        Long advId = paramMap.getLongMisValid("advId");
//        String content = paramMap.getStrMisValid("content");
//        Long replyUserCode = paramMap.getLongValid("replyUserCode");
//        Long userCode = jwtTokenUtil.getUserCodeFromToken();
//        WzUser myUser = cacheService.user(userCode);
//        if(myUser.getStatus() == 0){
//            return Res.fail("账户冻结中!");
//        }
//        WzAdvComment mainAdvComment = null;
//        //新建评论
//        WzAdvComment advComment = new WzAdvComment();
//        advComment.setAdvCommentId(IdWorker.getId());
//        advComment.setAdvId(advId);
//        advComment.setAvatarUrl(myUser.getAvatarUrl());
//        advComment.setContent(content);
//        advComment.setCreateTime(new Date());
//        advComment.setUserCode(userCode);
//        if(replyUserCode == null){//评论
//            advComment.setNickName(myUser.getNickName());
//            advComment.setMainUserCode(advComment.getAdvCommentId());
//            WzNews news = newsMapper.selectById(advId);
//        }else{//回复
////            Long advCommentId = paramMap.getLongMisValid("advCommentId");
////            WzAdvComment oldAdvComment = advCommentMapper.selectById(advCommentId);//上条回复
//            List<WzAdvComment> advCommentList = advCommentMapper.selectList(new EntityWrapper<WzAdvComment>().eq("user_code",replyUserCode).eq("adv_id",advId));
//            WzAdvComment oldAdvComment = advCommentList.get(0);
//            if(oldAdvComment == null){
//                return Res.fail(BizExceptionEnum.NEWS_SHIXIAO);
//            }
//            if(oldAdvComment.getMainUserCode() == null && oldAdvComment.getReplyUserCode() == null){
//                oldAdvComment.setMainUserCode(oldAdvComment.getAdvCommentId());
//            }
//            WzUser replyUser = cacheService.user(replyUserCode);
//            if (oldAdvComment.getMainUserCode().toString().equals(userCode.toString())) {//主回复人回复
//                advComment.setNickName(myUser.getNickName() +" 回复 "+replyUser.getNickName());
//            }else if(oldAdvComment.getMainUserCode().toString().equals(oldAdvComment.getAdvCommentId().toString())){//用户回复主回复人
//                advComment.setNickName(myUser.getNickName());
//            }else{//用户回复用户
//                advComment.setNickName(myUser.getNickName() +" 回复 "+replyUser.getNickName());
//            }
//            advComment.setMainUserCode(oldAdvComment.getMainUserCode());
//            advComment.setReplyUserCode(replyUserCode);
//            if(oldAdvComment.getReplyUserCode() == null){
//                mainAdvComment = oldAdvComment;
//            }else{
//                List<WzAdvComment> advComments = advCommentMapper.selectList(new EntityWrapper<WzAdvComment>().eq("adv_id",advId).isNull("reply_user_code").eq("main_user_code",oldAdvComment.getMainUserCode()));
//                mainAdvComment = advComments.get(0);
//            }
//            mainAdvComment.setHasUserComment(1);
//            mainAdvComment.setReplyNumber(mainAdvComment.getReplyNumber() +1);
//        }
//        int rr = advCommentMapper.insert(advComment);
//        if(rr > 0){
//            WzNews news = newsMapper.selectById(advId);
//            news.setReleaseTime(new Date());
//            rr = newsMapper.updateById(news);
//            if(rr > 0){
//                if(mainAdvComment != null){
//                    rr = advCommentMapper.updateById(mainAdvComment );
//                    if(rr <= 0){
//                        return Res.fail();
//                    }
//                }
//                return Res.success();
//            }
//        }
//        return Res.fail();
//    }

    /**
     * 删除资讯评论
     */
//    @Transactional
//    @PostMapping("commentDel")
//    public Res commentDel(@RequestBody ParamMap paramMap){
//        Long advCommentId = paramMap.getLongMisValid("advCommentId");
//        String advUserCode = paramMap.getStrMisValid("userCode");
//        Long userCode = jwtTokenUtil.getUserCodeFromToken();
//        if(!advUserCode.equals(userCode.toString())){
//            return Res.fail();
//        }
//        WzAdvComment advComment = advCommentMapper.selectById(advCommentId);
//        int r = 0;
//        if(advComment.getMainUserCode() == null){//原先的评论
//            r = advCommentMapper.deleteById(advComment);
//            if(r > 0){
//                return Res.success();
//            }
//        }
//        if(advComment.getAdvCommentId().toString().equals(advComment.getMainUserCode().toString())){//删除主评论
//            r = advCommentMapper.delete(new EntityWrapper<WzAdvComment>().eq("main_user_code",advComment.getAdvCommentId()));
//            if(r > 0){
//                return Res.success();
//            }
//        }else{//删除子评论
//            r = advCommentMapper.deleteById(advCommentId);
//            if(r > 0){
//                List<WzAdvComment> advComments = advCommentMapper.selectList(new EntityWrapper<WzAdvComment>().eq("adv_comment_id",advComment.getMainUserCode()));
//                if(advComments.size() > 0){
//                    advComments.get(0).setReplyNumber(advComments.get(0).getReplyNumber() - 1);
//                    if(advComments.get(0).getReplyNumber() < 0){
//                        advComments.get(0).setReplyNumber(0);
//                    }
//                    r = advCommentMapper.updateById(advComments.get(0));
//                    if(r > 0){
//                        return Res.success();
//                    }
//                }else{
//                    return Res.success();
//                }
//            }
//        }
//        return Res.fail();
//    }

    /**
     * 评论投诉
     */
//    @PostMapping("commentReport")
//    public Res commentReport(@RequestBody ParamMap paramMap){
//        String advCommentId = paramMap.getStrMisValid("advCommentId");
//        int reportType = paramMap.getIntMisValid("reportType");
//        Long userCode = jwtTokenUtil.getUserCodeFromToken();
//        PfReport report = new PfReport();
//        report.setCreateTime(new Date());
//        report.setReportType(reportType);
//        report.setTargetId(Long.parseLong(advCommentId));
//        report.setType(EnPfFile.REPORT_TYPE_newsComment.getIndex());
//        report.setUserCode(userCode);
//        int r = reportMapper.insert(report);
//        if(r > 0){
//            return Res.success();
//        }
//        return Res.fail();
//    }


    /**
     * 喜欢新闻
     */
//    @PostMapping("like")
//    public Res like(@RequestBody ParamMap paramMap){
//        String advId = paramMap.getStrMisValid("advId");
//        Long userCode = jwtTokenUtil.getUserCodeFromToken();
//        //业务处理
//        Res res = advService.newsCollection(paramMap,userCode, EnWzUser.USER_ADV_Like.getIndex());
//        return res;
//    }

    /**
     * 取消喜欢新闻
     */
//    @PostMapping("cancleLike")
//    public Res cancleLike(@RequestBody ParamMap paramMap){
//        String advId = paramMap.getStrMisValid("advId");
//        Long userCode = jwtTokenUtil.getUserCodeFromToken();
//        //业务处理
//        Res res = advService.cancleNewsCollection(paramMap,userCode,EnWzUser.USER_ADV_Like.getIndex());
//        return res;
//    }

    /**
     * 收藏新闻
     */
//    @PostMapping("collection")
//    public Res collection(@RequestBody ParamMap paramMap){
//        String advId = paramMap.getStrMisValid("advId");
//        Long userCode = jwtTokenUtil.getUserCodeFromToken();
//        //业务处理
//        Res res = advService.newsCollection(paramMap,userCode, EnWzUser.USER_ADV_Collection.getIndex());
//        return res;
//    }

    /**
     * 取消收藏新闻
     */
//    @PostMapping("cancleCollection")
//    public Res cancleCollection(@RequestBody ParamMap paramMap){
//        String advId = paramMap.getStrMisValid("advId");
//        Long userCode = jwtTokenUtil.getUserCodeFromToken();
//        //业务处理
//        Res res = advService.cancleNewsCollection(paramMap,userCode,EnWzUser.USER_ADV_Collection.getIndex());
//        return res;
//    }
}
